/*
* 复杂的对象类型推断
* 使用 ? 作为可选参数
* */

function showObj(obj: { id: number, name: string, fruits: { name: string, color: string }[], girlFriend?: string }) {
    let result = `${obj.name} 的 id 是 ${obj.id} ，最喜欢的水果是：`

    let list = ''
    for (let fruit of obj.fruits) {
        list += `名称 ${fruit.name}，颜色 ${fruit.color}`
    }
    console.log(result + list)
    if (obj.girlFriend) {
        console.log("补充，女朋友是", obj.girlFriend?.toLocaleLowerCase())
    }
}

const user = {
    id: 7,
    name: "罗俊华",
    fruits: [
        {
            name: "菠萝",
            color: "黄色"
        }, {
            name: "葡萄",
            color: "紫色"
        }
    ]
}
showObj(user)

showObj({
    ...user,
    girlFriend: "高仔AA"
})

// 给方法参数的类型 指定别名
type SimpleUser = {
    name: String
    age?: number | string,
    fruits: string[]
}

function showUserInfo(simpleUser: SimpleUser) {
    console.log(simpleUser.name, simpleUser.age, simpleUser.fruits)
}

showUserInfo({name: "罗俊华", fruits: ['苹果', '香蕉']})

